// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
// 引入element UI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App';
// 引入路由
import router from './router';
// 引入状态管理
import store from './vuex/store';
// 引入icon
import './assets/icon/iconfont.css'
// 引入echarts
import echarts from 'echarts'
import axios from 'axios';
// 过滤器
import * as custom from './utils/util'

import {me} from './api/userMG.js'

import vueAplayer from 'vue-aplayer'
import {setStore} from "./utils/util";
//

Vue.prototype.$echarts = echarts

Vue.prototype.$axios = axios;

Vue.prototype.$baseURL = "http://127.0.0.1:10002/";

Vue.config.productionTip = false;

// 使用element UI
Vue.use(ElementUI);

Object.keys(custom).forEach(key => {
  Vue.filter(key, custom[key])
});

Vue.filter('yesNo', function (data) {
  if (data === 0) {
    return '否';
  }
  if (data === 1) {
    return '是';
  }
});

Vue.filter('isReview', function (data) {
    return data ? '是' : '否'
  }
)


// 路由拦截器
router.beforeEach((to, from, next) => {
  if (!localStorage.getItem("userInfo")) {
    console.log("查询用户信息")
    me().then(resp => {
      if (resp.success) {
        setStore("userInfo", resp.data);
      }
      jumpPage(to, from, next);
    });
  } else {
    jumpPage(to, from, next);
  }

})

function jumpPage(to, from, next) {
  if (to.matched.length != 0) {
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
      next();
    } else {
      if (to.path != "/" && to.path != "/login") { //判断是否要跳到登录界面
        next();
      } else {
        next({
          path: '/home'
        })
      }
    }
  } else {
    next({
      path: '/login',
      query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
    })
  }
}

/* eslint-disable no-new */
let app = new Vue({
  el: '#app',
  router,
  store, //使用store vuex状态管理
  components: {App},
  template: '<App/>',
  data: {
    // 空的实例放到根组件下，所有的子组件都能调用
    Bus: new Vue()
  }

})
